﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Common
{
    /// <summary>
    /// 方便从字符串获取各种类型的值,或强制转换
    /// </summary>
    public static class TypeHelper
    {
       

        public static decimal? GetDec(string o)
        {
            decimal? tempDec = null;
            string oo = o.Trim();
            if (oo != null && oo != "-" && oo != "")
            {
                tempDec = decimal.Parse(o);
            }
            else if (oo != null && oo == "-" || oo == "")
            {
                tempDec = null;
            }
            //if(o != null)
            //{
            //    tempDec = decimal.Parse(o);
            //}
            return tempDec;
        }


        public static int? GetInt(string temp)
        {
            int? tempInt = null;
            if (temp != null)
            {
                if (temp.Trim() == string.Empty)
                {
                    return tempInt;
                }
                tempInt = int.Parse(temp);
            }
            return tempInt;
        }
        public static int? GetInt(object o)
        {
            int? tempInt = null;
            if (o != null)
            {
                tempInt = (int)o;
            }
            return tempInt;
        }
        public static string GetString(string temp)
        {
            if (temp != null)
            {
                temp = temp.Trim();
            }
            return temp;
        }

        public static string GetString(object o)
        {
            return Convert.ToString(o);
        }

        public static bool GetBool(string o)
        {
            return bool.Parse(o);
        }
        public static bool GetBool(object o)
        {
            return Convert.ToBoolean(o);
        }
        internal static DateTime? GetDateTime(string value)
        {
            DateTime? temp = null;
            if (value != null)
            {
                if (value.Trim()==string.Empty)
                {
                    return temp;
                }
                temp = DateTime.Parse(value);
            }
            return temp;
        }

        internal static DateTime? GetDateTime(object value)
        {
            DateTime? temp = null;
            if (value != null)
            {
                temp = (DateTime)(value);
            }
            return temp;
        }
    }
}
